import {http} from "../plugins/http";

const state = () => ({
    token: "",
    id: "",
    email: "",
    nickname: "",
    avatar: "",
    likeArticles: [],
    following: []
});

const mutations = {
    SET_TOKEN(state, token) {
        state.token = token;
    },
    SET_USER(state, user) {
        state.id = user._id;
        state.email = user.email;
        state.nickname = user.nickname;
        state.avatar = user.avatar;
        state.likeArticles = user.likeArticles;
        state.following = user.following;
    }
};
const actions = {
    login: async ({state, commit}, data) => {
        //     console.log("action data", data)
        let res = await http.post("/user/login", data);
        commit("SET_TOKEN", res.token);
        return res;
    },
    detail: async ({state, commit}, data) => {
        //     console.log("action data", data)
        let res = await http.get("/user/detail");
        console.log(res);
        if (res.code == 0) {
            commit("SET_USER", res.data);
            return res;
        }
    },
    logout: ({commit}) => {
        commit("SET_USER", {});
    }
};
export default {
    namespace: true,
    state,
    mutations,
    actions
};
